package com.example.datastoreutils

import com.example.tutorial.protos.AddressBook
import com.example.tutorial.protos.Person
import com.example.tutorial.protos.PersonKt.phoneNumber
import com.example.tutorial.protos.addressBook
import com.example.tutorial.protos.person
import kotlin.system.exitProcess

// This function fills in a Person message based on user input.
fun promptPerson(): Person = person {
    print("Enter person ID: ")
    id = readLine()!!.toInt()

    print("Enter name: ")
    name = readLine()!!

    print("Enter email address (blank for none): ")
    val email = readLine()!!
    if (email.isNotEmpty()) {
        this.email = email
    }

    while (true) {
        print("Enter a phone number (or leave blank to finish): ")
        val number = readLine()!!
        if (number.isEmpty()) break

        print("Is this a mobile, home, or work phone? ")
        val type = when (readLine()) {
            "mobile" -> Person.PhoneType.PHONE_TYPE_MOBILE
            "home" -> Person.PhoneType.PHONE_TYPE_HOME
            "work" -> Person.PhoneType.PHONE_TYPE_WORK
            else -> {
                println("Unknown phone type.  Using home.")
                Person.PhoneType.PHONE_TYPE_HOME
            }
        }
        phones += phoneNumber {
            this.number = number
            this.type = type
        }
    }
}

// Reads the entire address book from a file, adds one person based
// on user input, then writes it back out to the same file.
fun main() {
    promptPerson()
}
